#ifndef TCPSERVERBASE_H
#define TCPSERVERBASE_H

#include <QByteArray>
#include <QString>
#include <QTcpServer>
#include <QTcpSocket>
#include <QHostAddress>
#include <QMutex>
#include "tcpsendthread.h"
#include "../Include/PP_Const.h"

#define CLASS_NAME  TcpServerBase

#define TCPSERVER_DECL_SETGET(_NAME, _TYPE) \
                                    DECL_SETGET(_NAME, _TYPE)
#define TCPSERVER_METHOD_SETGET(_NAME, _TYPE, _VALUE)   \
                                    METHOD_SETGET(CLASS_NAME, _NAME, _TYPE, _VALUE)

#define SOCKET_DESCRIPTOR_ALL -1

typedef struct {
    int nSocketDescriptor;
    QByteArray qbaSendMessage;
}SendStruct;

class TcpServerBase : public QTcpServer
{
    Q_OBJECT
public:
    TcpServerBase();
    ~TcpServerBase();

    DECL_SETGET(Port, quint16)
    DECL_SETGET(HostAdd, QHostAddress)

    bool StartListen(QHostAddress haHostAdd = QHostAddress::Any, quint16 nPort = TCP_DEFAULT_PORT);
    void sendMessage(QByteArray qbaSendMessage, int nSocketDescriptor = SOCKET_DESCRIPTOR_ALL);

protected:
    virtual bool BeforeStartListen()                    {return false;}
    virtual bool AfterStartListen()                     {return false;}
    virtual void SuccessStartListen();
    //virtual bool DeMessage() = 0;

private:
    void DeleteInvalidSocket();

protected slots:
    virtual void slotNewComing();
    virtual void getMessage();
    //virtual void sendMessage(QByteArray qbaSendMessage, int nSocketDescriptor = SOCKET_DESCRIPTOR_ALL);

signals:
    void ReadyMessage();

private:
    quint16 m_nPort;
    QHostAddress m_haHostAdd;
    QList<QTcpSocket*> m_listTcpSocket;
    QList<QByteArray> m_listqaRecMessage;
    QList<SendStruct> m_listSendStruct;
    QMutex m_mutexSendLock;
    TcpSendThread* m_pMessageSend;
};

#endif // TCPSERVERBASE_H
